package biz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import dao.UserplusDao;
import entity.Userplus;
import util.PageSize;
import util.SessionFactory;

public class UserplusBiz {
	private UserplusDao userplusdao;
	
	public void insertUserplus(Userplus userplus) {
		SqlSession session = null;
		try {
			session = SessionFactory.SqlSessionFactory();
			userplusdao = session.getMapper(UserplusDao.class);
			userplusdao.insertUserplus(userplus);
			session.commit();
			session.close();
		}
		catch(Exception e) {
			session.rollback();
			e.printStackTrace();
		}
	}
	
	
	
	
	public int getPageCount() {
		SqlSession session = null;
		int pagecount = 0;
		try {
			session = SessionFactory.SqlSessionFactory();
			userplusdao = session.getMapper(UserplusDao.class);
			int recordcount = userplusdao.getRecordCount();
			if(recordcount%PageSize.pagesize==0)
				pagecount = recordcount/PageSize.pagesize;
			else
				pagecount = recordcount/PageSize.pagesize + 1;
			session.close();
		}
		catch(Exception e) {
			session.rollback();
			e.printStackTrace();
		}
		return pagecount;
	}
	public List<Userplus> selectAll(int curpage){
		List<Userplus> list = new ArrayList<Userplus>();
		SqlSession session = null;
		try {
			session = SessionFactory.SqlSessionFactory();
			userplusdao = session.getMapper(UserplusDao.class);
			int begin = (curpage - 1) * PageSize.pagesize;
			HashMap<String,Integer> map = new HashMap<String,Integer>();
			map.put("begin", begin);
			map.put("pagesize", PageSize.pagesize);
			list = userplusdao.selectAll(map);
			session.close();
		}
		catch(Exception e) {
			session.rollback();
			e.printStackTrace();
		}
		return list;
	}
	
	
	
	
	public void deleteUserById(int id) {
		SqlSession session = null;
		try {
			session = SessionFactory.SqlSessionFactory();
			userplusdao = session.getMapper(UserplusDao.class);
			userplusdao.delectUserplusById(id);
			session.commit();
			session.close();
		}
		catch(Exception e) {
			session.rollback();
			e.printStackTrace();
		}
	}
	
	
	
	
	public void updateUserById(String name,int age,int sex,int id) {
		SqlSession session = null;
		try {
			session = SessionFactory.SqlSessionFactory();
			userplusdao = session.getMapper(UserplusDao.class);
			HashMap map = new HashMap();
			map.put("name", name);
			map.put("age",age);
			map.put("sex",sex);
			map.put("id",id);
			userplusdao.updateUserplusById(map);
			session.commit();
			session.close();
		}
		catch(Exception e) {
			session.rollback();
			e.printStackTrace();
		}
	}
	public Userplus selectUserplusById(int id) {
		Userplus userplus = new Userplus();
		SqlSession session = null;
		try {
			session = SessionFactory.SqlSessionFactory();
			userplusdao = session.getMapper(UserplusDao.class);
			userplus = userplusdao.selectUserplusById(id);
			session.close();
		}
		catch(Exception e) {
			session.rollback();
			e.printStackTrace();
		}
		return userplus;
	}
	
}
